<?php
/*=============================================================================
|| ##################################################################
||	phpFoX Konsort
|| ##################################################################
||
||	Copyright		: (C) 2005-2007 PHPFOX Limited a Reality Fox Creation
||	Contact			: info@phpfox.com
||
||	- phpFoX and all of its source code and files are protected by Copyright Laws.
||
||	- The license for phpFoX permits you to install this software on a single domain (.com, .org, .net, etc.).
||
||	- You may also not remove this copyright screen which shows the copyright information and credits for phpFoX (RealityFOX Creations).
||
||	- phpFoX Konsort is NOT a FREE software - http://www.phpfox.com/license/
||
|| ##################################################################
=============================================================================*/

/* $File: ./include/modules/Gallery/classes/PhpFox_ComponentEditPictures.class.php, $Author:  $ */



/** Component for showing all user pictures and give ability for editing-deleting them
 *
 * @version $Id: PhpFox_ComponentEditPictures.class.php 297 2008-02-07 15:05:58Z phpfoxsv $
 * @package module.gallery
 */
class PhpFox_Mod_Gallery_ComponentEditPictures extends BaseComponent
{
    function process()
    {
    	App::membersOnly();

    	$oSrvAccount = &App::getModuleService('Account', 'Account');

        $oGalleryItem = &$this->_oModule->getItem('Gallery');
        /* @var $oGalleryItem PhpFox_Mod_Gallery_ItemGallery */
        $oGalleryService = &$this->_oModule->getService('Gallery');
        /* @var $oGalleryService PhpFox_Mod_Gallery_ServiceGallery */
        $oSecurityService = &App::getModuleService('Account', 'Security');
        /* @var $oSecurityService PhpFox_Mod_Account_ServiceSecurity */
        App::loadClass('Pager');
        App::loadClass('Text');
        $oTxt = &new Text();
        $oTpl = &$this->_oModule->getTemplate();
        $oReq = &App::getRequest();
        $oUrl = &App::getUrl();

        $oSecurityService->checkPerm('gallery', array('read_only', 'full'));
        $oSecurityService->checkAccess('gallery');
        $sUser = $oSecurityService->getCurrentUserLogin();

        if ($iItemId = $oReq->get('delete'))
        {
            if (!$oSecurityService->checkPerm('gallery', 'full', false))
            {
                App::gotoUrl();
            }
            if (!$oGalleryItem->load($iItemId))
            {
                App::gotoUrl();
            }
            if (App::getUser('id') != $oGalleryItem->aData['user_id'])
            {
                App::gotoUrl();
            }
            $oGalleryService->deleteItem($iItemId);
            $oUrl->clearParams('delete');
            App::forward($oUrl->getUrl());
        }
        if ($oReq->getAction() == 'update')
        {
        if (!$oSecurityService->checkPerm('gallery', 'full', false))
            {
                App::gotoUrl();
            }
            if (!$iId = $oReq->getInt('id'))
            {
                App::gotoUrl('public.gallery');
            }
            $aValues = $oReq->getArray('val');
            $oGalleryItem->load($iId);
            if ($oGalleryItem->aData['user_id'] != App::getUser('id'))
            {
                App::gotoUrl('public.gallery');
            }
            $oGalleryItem->aData['text'] = $oTxt->parseText($aValues['title']);
            $oGalleryItem->aData['category'] = $aValues['category'];
            $oGalleryItem->aData['who_view'] = $aValues['who_view'];
            if (App::getSetting('approve_images'))
            {
                if (($oGalleryItem->aData['who_view'] != '0') && ($aValues['who_view'] == '0'))
                {
                    $oGalleryItem->aData['allow'] = 1;
                }
            }
            $oGalleryItem->aData['tags']      = strip_tags($oTxt->preParseText($oTxt->ShortenText($aValues['tags'], 75)));
            $oGalleryItem->update(array('text', 'category', 'who_view', 'allow', 'tags'));
            $oUrl->clearParams('id');
            App::forward($oUrl->getUrl());
        }
        $iPage = $oReq->get('page', 1);
        $iPageSize = App::getParam('public.pagesize');
        $aCond = array();
        $aCond[] = 'ma.user_id = "'. App::getUser('id') .'"';
        $sSort = 'id DESC';
        list($aItems, $iCnt) = $oGalleryItem->getList($aCond, $iPage, $iPageSize, $sSort);
        $oPager = &new Pager($iCnt, $iPage, $iPageSize);
        $iFrom = $oPager->getOffset($iPage, $iPageSize);
        $iTo = (($iFrom + $iPageSize) > $iCnt) ? $iCnt : ($iFrom + $iPageSize);
        $oTpl->assign(array(
        	'aItems'      => $aItems,
        	'iCnt'        => $iCnt,
        	'iFrom'       => ($iCnt) ? ($iFrom + 1) : 0,
        	'sGImageUrl'  => App::getParam('gallery.pic.url'),
        	'iTo'         => $iTo,
        	'aPaging'     => ($iCnt) ? $oPager->getInfo($oUrl) : '',
        	'aCategories' => App::getOptions('image_cat'),
        	'aPrivacySettings' => $oSrvAccount->getPrivacySettings()
        ));
        return $oTpl->fetch('EditPictures.html');
    }
}
?>